package com.itboye.mengwu.app.ui.aiwujia;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import java.util.zip.Inflater;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.interfaces.IFenSiInterface;
import com.itboye.mengwu.data.model.MaMaZengFaBuFenLeiBean;
import com.itboye.mengwu.data.model.WodeZengSongShangPingBean;
import com.itboye.mengwu.data.presenter.NewMaMaZengPresenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ActivityMaMaZengFaBuFenLei extends BaseActivity implements Observer {

	ListView daLv, zhongLv, xiaoLv;
	NewMaMaZengPresenter newMaMaZengPresenter;
	ArrayList<MaMaZengFaBuFenLeiBean> maMaZengFaBuFenLeiBean;
	MyDaAdapter myDaAdapter;
	MyZhongAdapter myZhongAdapter;
	MyXiaoAdapter myXiaoAdapter;

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = super.handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == NewMaMaZengPresenter.LeimubufenyeSuccess) {
				maMaZengFaBuFenLeiBean = (ArrayList<MaMaZengFaBuFenLeiBean>) entity.getData();
				myDaAdapter = new MyDaAdapter(maMaZengFaBuFenLeiBean, this);
				daLv.setAdapter(myDaAdapter);
				zhongLv.setVisibility(View.GONE);
				xiaoLv.setVisibility(View.GONE);
			} else if (entity.getEventType() == NewMaMaZengPresenter.LeimubufenyeFail) {

			} else {

			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public int initView() {
		return R.layout.activity_mamazengfabu_fenlei;
	}

	@Override
	public void onMyCreate() {
		newMaMaZengPresenter = new NewMaMaZengPresenter(this);
		newMaMaZengPresenter.leimubufenye();
		showProgressDialog("正在加载中",true );
	}

	// 最左边的listview的adapter
	public class MyDaAdapter extends BaseAdapter {
		ArrayList<MaMaZengFaBuFenLeiBean> maMaZengFaBuFenLeiBean;
		Context context;

		public MyDaAdapter() {
			super();
		}

		public MyDaAdapter(ArrayList<MaMaZengFaBuFenLeiBean> maMaZengFaBuFenLeiBean, Context context) {
			this.maMaZengFaBuFenLeiBean = maMaZengFaBuFenLeiBean;
			this.context = context;
		}

		@Override
		public int getCount() {
			return maMaZengFaBuFenLeiBean != null ? maMaZengFaBuFenLeiBean.size() : 0;
		}

		@Override
		public Object getItem(int position) {
			return maMaZengFaBuFenLeiBean != null ? maMaZengFaBuFenLeiBean.get(position) : null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.item_mamazeng_fabu_fenlei, null);
				holder.tv = (TextView) convertView.findViewById(R.id.tv);
				holder.line = (LinearLayout) convertView.findViewById(R.id.line);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.tv.setText(maMaZengFaBuFenLeiBean.get(position).getName());

			holder.line.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (maMaZengFaBuFenLeiBean.get(position) != null
							&& maMaZengFaBuFenLeiBean.get(position).getChild().size() > 0) {
						myZhongAdapter = new MyZhongAdapter(maMaZengFaBuFenLeiBean, context, position);
						zhongLv.setAdapter(myZhongAdapter);
						zhongLv.setVisibility(View.VISIBLE);
					} else {
						ByAlert.alert(maMaZengFaBuFenLeiBean.get(position).getName());
//						Intent intent = new Intent(ActivityMaMaZengFaBuFenLei.this, ActivityAiwujiaMomPublish.class);
						Intent intent =new Intent();
						intent.putExtra("cate_id", maMaZengFaBuFenLeiBean.get(position).getId());
						intent.putExtra("cate_name", maMaZengFaBuFenLeiBean.get(position).getName());
//						startActivity(intent);
						setResult(101, intent); 
						finish();
					}

				}
			});
			return convertView;

		}
	}

	// 中间的listview的adpter
	class MyZhongAdapter extends BaseAdapter {
		ArrayList<MaMaZengFaBuFenLeiBean> entity;
		Context context;
		int daPosition;

		public MyZhongAdapter() {
			super();
		}

		public MyZhongAdapter(ArrayList<MaMaZengFaBuFenLeiBean> entity, Context context, int daPosition) {
			super();
			this.entity = entity;
			this.context = context;
			this.daPosition = daPosition;
		}

		@Override
		public int getCount() {
			return entity != null ? entity.get(daPosition).getChild().size() : 0;
		}

		@Override
		public Object getItem(int position) {
			return entity != null ? entity.get(daPosition).getChild().get(position) : null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.item_mamazeng_fabu_fenlei, null);
				holder.tv = (TextView) convertView.findViewById(R.id.tv);
				holder.line = (LinearLayout) convertView.findViewById(R.id.line);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.tv.setText(entity.get(daPosition).getChild().get(position).getName());
			holder.line.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (entity.get(daPosition).getChild().get(position).getChild() != null
							&& entity.get(daPosition).getChild().get(position).getChild().size() > 0) {
						myXiaoAdapter = new MyXiaoAdapter(entity.get(daPosition).getChild().get(position), context,
								daPosition, position);
						xiaoLv.setAdapter(myXiaoAdapter);
						xiaoLv.setVisibility(View.VISIBLE);

					} else {
						ByAlert.alert(maMaZengFaBuFenLeiBean.get(daPosition).getName()
								+ maMaZengFaBuFenLeiBean.get(daPosition).getChild().get(position).getName());
//						Intent intent = new Intent(ActivityMaMaZengFaBuFenLei.this, ActivityAiwujiaMomPublish.class);
						Intent intent =new Intent();
						intent.putExtra("cate_id",
								maMaZengFaBuFenLeiBean.get(daPosition).getChild().get(position).getId());
						intent.putExtra("cate_name",
								maMaZengFaBuFenLeiBean.get(daPosition).getChild().get(position).getName());
//						startActivity(intent);
						setResult(101, intent); 
						finish();
					}

				}
			});
			return convertView;
		}

	}

	// 最右边的listView的Adpter
	class MyXiaoAdapter extends BaseAdapter {
		MaMaZengFaBuFenLeiBean.B entity;
		private LayoutInflater inflater;
		Context context;
		int bigPosition, zhongPosition;

		public MyXiaoAdapter() {
			super();
		}

		public MyXiaoAdapter(MaMaZengFaBuFenLeiBean.B entity, Context context, int bigPosition, int zhongPosition) {
			super();
			this.entity = entity;
			this.context = context;
			this.bigPosition = bigPosition;
			this.zhongPosition = zhongPosition;
		}

		@Override
		public int getCount() {
			return entity.getChild() != null ? entity.getChild().size() : 0;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return entity.getChild() != null ? entity.getChild().get(position) : null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.item_mamazeng_fabu_fenlei, null);
				holder.tv = (TextView) convertView.findViewById(R.id.tv);
				holder.line = (LinearLayout) convertView.findViewById(R.id.line);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.tv.setText(entity.getChild().get(position).getName());
			holder.line.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					ByAlert.alert(maMaZengFaBuFenLeiBean.get(bigPosition).getName()
							+ maMaZengFaBuFenLeiBean.get(bigPosition).getChild().get(zhongPosition).getName()
							+ maMaZengFaBuFenLeiBean.get(bigPosition).getChild().get(zhongPosition).getChild()
									.get(position).getName());
//					Intent intent = new Intent(ActivityMaMaZengFaBuFenLei.this, ActivityAiwujiaMomPublish.class);
					 Intent intent=new Intent();  
					intent.putExtra("cate_id", maMaZengFaBuFenLeiBean.get(bigPosition).getChild().get(zhongPosition)
							.getChild().get(position).getId());
					intent.putExtra("cate_name", maMaZengFaBuFenLeiBean.get(bigPosition).getChild().get(zhongPosition)
							.getChild().get(position).getName());
					setResult(101, intent);  
					
					finish();
				}
			});

			return convertView;
		}

	}

	class ViewHolder {

		public TextView tv;
		public LinearLayout line;
	}
	
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		 
//        if (keyCode == KeyEvent.KEYCODE_BACK
//                 && event.getRepeatCount() == 0) {
//            //do something...
//        	
//        	finish();
//             return true;
//         }
//         return super.onKeyDown(keyCode, event);
//     }


}
